﻿package lobby
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.NetConnection;
	import flash.text.TextField;
	import lobby.events.LobbyEvent;
	import lobby.GlobalVariables;
	import lobby.exchangeddata.HashTable;
	import flash.ui.Keyboard;
	import lobby.component.combobox.*;
	
	/**
	 * ...
	 * @author Tom
	 */
	
	public class LoginPanel extends MovieClip
	{
		public var loginMenu_txt:TextField;
		public var msg_txt:TextField;
		public var lang_txt:TextField;
		public var user_txt:TextField;
		public var pass_txt:TextField;
		public var fldUserId:TextField;
		public var fldPassword:TextField;
		public var fldCaptcha:TextField;
		public var btnLogin:SimpleButton;
		public var login_txt:TextField;
		private var capt:Captcha;
		public var captcha_mc:MovieClip;
		public var captWidth:int;
		public var captHeight:int;
		public var langCboBox:PannelCombobox;
		
		public function LoginPanel()
		{
			langCboBox.tabChildren = false;
			fldUserId.tabIndex = 1;
			fldPassword.tabIndex = 2;
			fldCaptcha.tabIndex = 3;
			fldCaptcha.restrict = "0-9";
			btnLogin.tabIndex = 4;
			stage.focus = fldUserId;
			
			capt = new Captcha(captcha_mc.width, captcha_mc.height);
			captcha_mc.addChild(capt);
			capt.initCaptcha();
			login_txt.mouseEnabled = false;
			this.btnLogin.addEventListener(MouseEvent.CLICK, hdlClick);
			fldUserId.addEventListener(KeyboardEvent.KEY_DOWN, hdlPressEnter);
			fldPassword.addEventListener(KeyboardEvent.KEY_DOWN, hdlPressEnter);
			fldCaptcha.addEventListener(KeyboardEvent.KEY_DOWN, hdlPressEnter);
			captcha_mc.addEventListener(MouseEvent.CLICK, changeCaptcha);
			
			//combo box
			initLangCombo();
		}
		
		private function initLangCombo():void
		{
			var dataCombobox:Array = new Array();
			var obj:Object = new Object();
			obj["name"] = "G";
			obj["value"] = "简体中文"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "B";
			obj["value"] = "繁體中文"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "T";
			obj["value"] = "ภาษาไทยv"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "K";
			obj["value"] = "한국어"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "J";
			obj["value"] = "日本語"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "V";
			obj["value"] = "Tiếng Việt"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "M";
			obj["value"] = "Indonesia"
			dataCombobox.push(obj);
			obj = new Object();
			obj["name"] = "E";
			obj["value"] = "English"
			dataCombobox.push(obj);
			langCboBox.addEventListener(EventCombobox.COMPLETED_INIT_ITEM, hdlInitCompleted);
			langCboBox.initCombobox(dataCombobox, "name", "value");
			langCboBox.addEventListener(EventCombobox.CHANGED, hdlSelect);
		}
		
		private function hdlInitCompleted(e:EventCombobox):void
		{
			langCboBox.setDefaultItem(GlobalVariables.lang);
		}
		
		private function hdlSelect(e:EventCombobox):void
		{
			GlobalVariables.lang = e.currentName;
			GlobalVariables.langInfo.updateLangInfo(GlobalVariables.lang);
			changeTextLang(GlobalVariables.langInfo.hashTable);
		}
		
		private function hdlClick(e:MouseEvent):void
		{
			doLogin();
		}
		
		private function hdlPressEnter(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				doLogin();
			}
		}
		
		private function changeCaptcha(e:MouseEvent):void
		{
			capt.initCaptcha();
		}
		
		private function doLogin():void
		{
			var event:LobbyEvent;
			event = new LobbyEvent(LobbyEvent.SET_ERROR_MESSAGE, true);
			if (fldUserId.text != "" && fldPassword.text != "")
			{
				if (fldCaptcha.text != "" && capt.checkCaptcha(fldCaptcha.text))
				{
					GlobalVariables.userName = fldUserId.text;
					GlobalVariables.pass = fldPassword.text;
					dispatchEvent(new LobbyEvent(LobbyEvent.CALL_LOGIN, true));
				}
				else
				{
					capt.initCaptcha();
					stage.focus = fldCaptcha;
					event.result = "InvalidCaptcha";
					dispatchEvent(event);
				}
			}
			else
			{
				capt.initCaptcha();
				stage.focus = fldUserId;
				event.result = "RequireUserIdPassword";
				dispatchEvent(event);
			}
		}
		
		public function changeTextLang(hashTableInfo:HashTable):void
		{
			loginMenu_txt.text = hashTableInfo.lookupString("Lologin").toUpperCase();
			msg_txt.text = hashTableInfo.lookupString("RequireUserIdPassword");
			lang_txt.text = hashTableInfo.lookupString("availablelanguage");
			user_txt.text = hashTableInfo.lookupString("Lousername");
			pass_txt.text = hashTableInfo.lookupString("Lopassword");
			login_txt.text = hashTableInfo.lookupString("Lologin");
		}
		
		public function resetText():void
		{
			fldUserId.text = "";
			fldPassword.text = "";
			fldCaptcha.text = "";
			stage.focus = fldUserId;
			capt.initCaptcha();
		}
		
		public function updateComboBox():void
		{
			langCboBox.setDefaultItem(GlobalVariables.lang);
		}
	}

}